<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <#include "/common/plateform/include/common-base.ftl">
  <title>机构管理</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>
<body class="hold-transition ${sysColor} sidebar-mini">
<div class="wrapper">

  <!-- top start -->
	<#include "/common/plateform/top.ftl">
  <!-- top end -->
  <!-- left start -->
	<#include "left-account.ftl">
  <!-- left end-->

  <div class="content-wrapper">
		
    <!-- Main start -->

<section class="content">
      <div class="row">
        <div class="col-xs-12">
        
         <!-- 机构搜索 start -->
           <div class="box box-info">
             <!--搜索title  start --> 
	          <div class="box-header with-border">
                 <h3 class="box-title"><i class="fa fa-search"></i> 搜索</h3>
                 <div class="box-tools pull-right">
	                <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
	                </button>
	                <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
                 </div>
              </div>
            <!--搜索title  end --> 
            <div class="box-body">
			  <form id="searchFrom" class="form-horizontal">
				<input type="hidden" name="doCode" value="orgList"/>
				<input type="hidden" name="fatherId" value="0"/>
				
				<div class="form-group">
					<div class="col-sm-3 text_r"><label class="control-label">机构标识：</label></div>
					<div class="col-sm-2">
						 <input class="form-control" name="orgEname"  value="" type="text">
					</div>
					<div class="col-sm-2 text_r"><label class="control-label">机构姓名：</label></div>
					<div class="col-sm-2">
						 <input class="form-control"name="orgCname" value="" type="text">
					</div>
				</div>
				<center>
				   <div class="box-footer clearfix">
				       <button type="button" class="btn btn-success" onclick="getTableData();"><i class="fa fa-search"></i> 查询</button>&nbsp;
				       <button type="button" class="btn btn-warning" onclick="cleanData('searchFrom');">重置 </button>
				   </div>
				</center>
			  </form>
            </div>
	     </div>
         <!-- 机构搜索 end -->
        
          <div class="box box-success">
            <div class="box-header with-border">
                 <h3 class="box-title"><i class="fa fa-table"></i> 机构管理　　
					 <#if (loginUser.sysFlag=='1') >　
					  <button class="btn btn－primary" type="button" onclick="toAdd('parent');"><span aria-hidden="true" class="glyphicon glyphicon-plus"></span>增加机构</button>
		              <button class="btn btn-info" type="button" onclick="toAdd('child');">增加子机构</button>
					 </#if>
					 <#if (loginUser.sysFlag=='2') >　
						 <button class="btn btn-info" type="button" onclick="toAdd('child');">增加子机构</button>
					 </#if>
					 <#if (loginUser.sysFlag=='1' || loginUser.sysFlag =='2') >　
		            	<button class="btn btn-danger" type="button" onclick="del();">删除</button>
		            	<button class="btn btn-warning" type="button" onclick="toEdit();">修改</button>
		            	<button class="btn btn-success" type="button" onclick="popDept();">部门管理</button>
					 </#if>
		            	<button class="btn btn-info" type="button" onclick="toOrgUser();">机构人员</button>
					 <#if (loginUser.sysFlag=='1' || loginUser.sysFlag =='2') >　
		            	<button class="btn btn-info" type="button" onclick="popRole();">角色设置</button>
		            	<button class="btn btn-info" type="button" onclick="popOrgActs();">资源设置</button>
					 </#if>
                 </h3>
                 <div class="box-tools pull-right">
	                <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
	                </button>
	                <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
                 </div>
              </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="example" class="table table-bordered" >
                <thead>
                <tr>
              	  <th>机构名称</th>
                  <th>机构标识</th>
                  <th>子节点数</th>
                  <th>英文全称</th>
                  <th>中文全称</th>
                  <th>当前状态</th>
                </tr>
                </thead>
                <tbody id="myList">
                </tbody>
              </table>
                <center>
    	  			  <ul id="pageList"></ul>
        		</center>
            </div>
          </div>
        </div>
      </div>
    </section>
    <!-- Main end -->
    
    
 <div class="modal fade" id="my_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  	
	<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="modal-title">
							<font color="red">机构信息</font>&nbsp;
						</h4>
					</div>
					<div class="modal-body">
						<form class="form-horizontal" role="form" id="orgForm">
							<input type="hidden" name="formToken" id="formToken" value="${formToken}">
							<input type="hidden" name="doCode" value="orgSave"/>
							<input type="hidden" name="orgId" id="orgId" class="clean" value="">
							<input type="hidden" name="fatherId" id="fatherId" class="clean" value=""/>
							<input type="hidden" name="updateCount" id="updateCount" value=""/>
							<input type="hidden" name="subCount" id="subCount" value=""/>
							<input type="hidden" name="treeLevel" class="clean" id="treeLevel" value="1"/>
							<input type="hidden" id="org_ifSaveDept" name="ifSaveDept" value="T"/>
							<div class="box-body" id="orgFormData">
			                <div class="form-group hidden">
			                  <label for="inputOrgCname" class="col-sm-2 control-label" >所属系统</label>
			                  <div class="col-sm-8">
			                  	<select name="sysEname" id="sysEname" class="form-control"></select>
			                  </div>
			                </div>
							<div class="form-group">
								<label class="col-sm-2 control-label" >上级机构</label>
								<div class="col-sm-8 form-control-static">
									<span id="orgForm_father_org_cname"></span>
								</div>
							</div>
			                <div class="form-group">
			                  <label for="inputOrgCname" class="col-sm-2 control-label" >机构名称</label>
			                  <div class="col-sm-8">
			                    <input type="text" class="form-control" id="orgCname"  name="orgCname" required placeholder="必填项">
			                  </div>
			                </div>
			                <div class="form-group">
			                  <label class="col-sm-2 control-label">机构标识</label>
			                  <div class="col-sm-8">
			                    <input type="text" class="form-control" id="orgEname" onblur="checkOrgEnameIsUse(this)"  name="orgEname" required placeholder="必填项">
			                  </div>
			                </div>
			                
			                <div class="form-group">
			                  <label class="col-sm-2 control-label">机构代码</label>
			                  <div class="col-sm-8">
			                    <input type="text" class="form-control" id="orgCode" name="orgCode" required placeholder="必填项">
			                  </div>
			                </div>
			                
			                <div class="form-group">
			                  <label class="col-sm-2 control-label">联系人</label>
			                  <div class="col-sm-8">
			                    <input type="text" class="form-control" id="linkMan" name="linkMan" placeholder="">
			                  </div>
			                </div>
			                
			                <div class="form-group">
			                  <label class="col-sm-2 control-label">联系电话</label>
			                  <div class="col-sm-8">
			                    <input type="text" class="form-control"  id="phone" name="phone" placeholder="">
			                  </div>
			                </div>
			                
			                <div class="form-group">
			                  <label class="col-sm-2 control-label">所在地区</label>
			                  <div class="col-sm-8">
			                    <input type="text" class="form-control" id="address" name="address" required placeholder="必填项">
			                  </div>
			                </div>
			                
			                <div class="form-group">
			                  <label class="col-sm-2 control-label">状态</label>
			                  <div class="col-sm-8">
			                    <input type="radio" checked="checked" value="1" name="orgStatus"> <i></i> 启用 
                                <input type="radio" value="0" name="orgStatus"> <i></i> 停用
			                  </div>
			                </div>
			                
			              </div>
			               <div class="box-footer">
			                <button type="button" class="btn btn-primary pull-left" onclick="cannel('my_modal');">取消</button>
			                <button type="button" class="btn btn-primary pull-right" onclick="save('orgForm');">提 交</button>
			              </div>
			              
						</form>
					</div>
				</div>
				<!-- /.modal-content -->
			</div>		
</div>  
 <!-- 部门管理 -->
<div class="modal fade" id="dept_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog" style="width:900px;">
		<div class="modal-content" id="orgDeptDiv"></div>
	</div>
</div>	

<!-- 机构用户 -->
<div class="modal fade" id="org_user_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog" style="width:900px;">
				<div class="modal-content" id="orgUserDiv"></div>
	</div>
	<!-- /.modal-content -->
</div>	 

<div class="modal fade" id="role_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="overflow: auto;">
	<div class="modal-dialog" style="width:1000px;">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">
							<font color="red">角色管理【<label id="roleLab"></label>】</font>&nbsp;
						</h4>
					</div>
					<div class="modal-body" id="org-role-list" ></div>
				</div>
	</div>
				<!-- /.modal-content -->
</div>	    
    
  
<!-- 角色权限设置 -->
<div class="modal fade" id="role_acts_modal" tabindex="-1" role="dialog" style="overflow: auto;">
	<div class="modal-dialog" style="width:1000px;">
				<div class="modal-content">
					<div class="modal-body" id="roleActsDiv"></div>
				</div>
	</div>
</div>

<div class="modal fade" id="org_acts_modal" bindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog" style="width:1000px;">
		<div class="modal-content" id="orgActsDiv"></div>
	</div>
	<!-- /.modal-content -->
</div>	
 
<div class="modal fade" id="dept_users_modal" bindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog" style="width:1000px;">
		<div class="modal-content" id="deptUsersDiv"></div>
	</div>
	<!-- /.modal-content -->
</div>  
<div class="modal fade" id="dept_acts_modal" bindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog" style="width:1000px;">
		<div class="modal-content" id="deptActsDiv"></div>
	</div>
	<!-- /.modal-content -->
</div>    
    
  <script type="text/template" id="dataList">
	  		<? for ( var i = 0; i < rows.length; i++) {?>
				<tr id=<?=rows[i].orgId?> treeLevel=<?=rows[i].treeLevel?> fatherId=<?=rows[i].fatherId?>>
					<th style="display:none;"><?=rows[i].orgId?></th>
					<th style="display:none;"><?=rows[i].orgCname?></th>
					<th style="display:none;"><?=rows[i].treeLevel?></th>
					<th style="display:none;"><?=rows[i].fatherId?></th>
					<td>

						<?if(rows[i].subCount>0){?>
							<i class="fa fa-folder fa-cyellow" onclick="getChildTableData('<?=rows[i].orgId?>','<?=rows[i].treeLevel?>',this);"></i>&nbsp;<?=rows[i].orgCname?>
						<?} else{?>		
							<i class="fa fa-file-text fa-cyellow" ></i>&nbsp;<?=rows[i].orgCname?>
						<?}?>		

					</td>
					<td><?=rows[i].orgEname?></td>
					<td><?=strUtil.doNull(rows[i].subCount,'')?></td>
					<td><?=rows[i].fullEname?></td>
					<td><?=rows[i].fullCname?></td>
					<td>
						<?if(rows[i].orgStatus=='1'){?>
						启用
						<?}?>
						<?if(rows[i].orgStatus=='0'){?>
						停用
						<?}?>
					</td>
				</tr>
     		<?}?>
	</script>
    
    
 <script>
var clickMenu= 'org-list';
var id;
var ename;
var cname;
var treeLevel;
var index;
var sysUser='${loginUser.sysFlag}';
var deptNum;//当前选中的部门属于第几行
$(function () {
	getTableData();
	getSysEname();
	$('#example tbody').on('click','tr', function (e) {
	     if ( $(this).hasClass('selected') ) {
	            //$(this).removeClass('selected');
	        }  else {
	            $('tr.selected').removeClass('selected');
	            $(this).addClass('selected');
	        }
	     id = $('tr.selected th').eq(0).html();
	     ename = $('tr.selected td').eq(1).html();
	     orgId = $('tr.selected th').eq(0).html();
	     cname = $('tr.selected th').eq(1).html();
	     treeLevel= $('tr.selected th').eq(2).html();
	     fatherId= $('tr.selected th').eq(3).html();
	   //  console.log($('tr.selected td').eq(1).html())
	} );
  });
//设置模态框隐藏事件钩子  
$( '#my_modal' ).on( 'hide.bs.modal' ,function(e){  
	cleanData('orgFormData');
}); 
//设置模态框隐藏事件钩子  
$( '#my_modal' ).on( 'show.bs.modal' ,function(e){  
	$.ajax({type:'get',dataType:'text',
	    url:'${ctx}/sendPage/token?token=true&date='+new Date().getTime(),
	    success: function(myData){ $('#formToken').val(myData);}
	});	
}); 
  
function cleanData(id){
	$(':input','#'+id)  
	 .not(':button, :submit, :reset, :hidden,:radio')  
	 .val('')  
	 .removeAttr('selected'); 
}

function getTableData(){
	$.ajax({ type:'get', url:common_list_url, dataType:'json',
		   data:$('#searchFrom').serialize()+getCurrentPage(common_list_url+"?"+$('#searchFrom').serialize(),10),
		   success: function(myData){
			//console.log(myData);
			var data = baidu.template('dataList',myData);  
			$("#myList").html(data);
			 var option= {
					   ele:$("#pageList")[0],
					   total:myData.total,
					   fn:getTableData,
					   url:common_list_url+"?"+$('#searchFrom').serialize()
				   }
			generatePages(option);
		   }
		});	
  }
  
function getChildTableData(id,treeLevel,obj){
	$.ajax({
		   type:'get',
		   url:common_list_url,
		   data:{'fatherId':id,'doCode':'orgList'},
		   dataType:'json',
		   success: function(myData){
			var data = baidu.template('dataList',myData);  
			if($(obj).hasClass("fa-folder-open")){
				$(obj).attr("class","fa fa-folder  fa-cyellow");
				delChiled(id);
			}else{
				$(obj).attr("class","fa fa-folder-open fa-cyellow");
				$("#"+id).after(data);
				 $("[fatherid='"+id+"']").each(function(i){
					var thisObj = $(this);
					var pd = "10px 20px 10px ";
					var num = pd+parseInt(treeLevel)*20+"px";
					$(thisObj).find("td").eq(0).css("padding",num);
				 });
			 }
		   }
		});	
  } 
  
  function delChiled(id){
	  $("[fatherid='"+id+"']").each(function(i){
			 var thisObj = $(this);
			 $(thisObj).remove();
			 var id = thisObj.attr("id");
			 delChiled(id);
		 });
  }
  
function toAdd(val){
	mytools.cleanData("#orgForm");
	if(val=='child'){
		if(id=="undefined" || id =="" || id == null){
	 		layer.alert('请先选中一条数据！', { closeBtn: 0 });
			return;
		}else{
			$("#fatherId").val(id);
			$("#treeLevel").val(parseInt(treeLevel)+1);
			$("#orgForm_father_org_cname").text(cname);
		}
	}
	if(val=='parent'){
		$("#fatherId").val(0);
		$("#orgId").val("");
		$("#treeLevel").val(1);
        $("#orgForm_father_org_cname").text("无");
	}
	$('#my_modal').modal('show');
}

function popDept(){
	if(id=="undefined" || id =="" || id == null){
		alert("请先选中一条数据！");
		return;
	}else{
		$("#orgDeptDiv").load("${ctx}/template/uct/org-dept.html",function(){
			$("#orgLab").html(cname);
			getDeptList(orgId,null);
			$('#deptTable tbody').on('click','tr', function (e) {
			     if ( $(this).hasClass('selected') ) {
			        } else {
			            $('tr.selected').removeClass('selected');
			            $(this).addClass('selected');
			        }
			     deptNum = $('tr.selected th').eq(0).html();
			} );
			
			$('#dept_modal').modal('show');

		});
	
	}
}
//

function popRole(){
	if(id=="undefined" || id =="" || id == null){
		alert("请先选中一条数据！");
		return;
	}else{
		$("#org-role-list").load("${ctx}/template/uct/org-role-list.html",function(){getRoleList(orgId,fatherId,'${ctx}');
		$("#roleLab").html(cname);
		$('#role_modal').modal('show');
		$('#roleTable tbody').on('click','tr', function (e) {
		     if ( $(this).hasClass('selected') ) {
		        }  else {
		            $('tr.selected').removeClass('selected');
		            $(this).addClass('selected');
		        }
		     deptNum = $('tr.selected th').eq(0).html();
		} );
		});
	}
}

function popOrgActs(){
	if(id=="undefined" || id =="" || id == null){
		alert("请先选中一条数据！");
		return;
	}else{
		$("#orgActsDiv").load("${ctx}/template/uct/org-acts.html",function(){getOrgActListData(orgId,fatherId);
		$("#roleLab").html(cname);
		$('#org_acts_modal').modal('show');
		$('#roleTable tbody').on('click','tr', function (e) {
		     if ( $(this).hasClass('selected') ) {
		        }  else {
		            $('tr.selected').removeClass('selected');
		            $(this).addClass('selected');
		        }
		     deptNum = $('tr.selected th').eq(0).html();
		} );
		});
	}
}


function toEdit(){
	if(id=="undefined" || id =="" || id == null){
		alert("请先选中一条数据！");
		return;
	}else{
		$.ajax({ type:'get', url:common_info_url,dataType:'json',
		    data:{'orgId':id,'doCode':'orgInfo'},
		    success: function(myData){
		    	 console.log(myData);
				 FormUtil.fillData("#orgForm", myData,"id");
				 DictUtil.checkRadioDict("orgStatus", myData.orgStatus);
				 $('#my_modal').modal('show');
		  	}
		});	
	}
	//$('#my_modal').modal('show');
}

function del(){
	if(strUtil.isNull(id)){
		layer.alert("请先选中一条数据！");
		return;
	}else{
		if(ename=="platform"){
			layer.alert("此机构不能删除！");
			return;
		}
	    if(id=="${loginUser.orgId}"){
            layer.alert("自己不能删除自己机构！");
	        return;
		}
		var index_submit = layer.confirm("您确定要删除["+ename+"]吗？", {btn: ["Yes","No"],closeBtn: 0,offset: "220px"}
		, function(){
			layer.close(index_submit);
			$.ajax({ type:'post',url:common_submit_url,dataType:'json',
			    data:{'orgId':id,'doCode':'orgDel'},
			    success: function(myData){
			    	layer.alert(myData.msg, { closeBtn: 0 });
			    	if(myData.flag=='T'){
			    		refresh();
			    	}
			  	}
			});	
		}, function(){
		});
		
	}
}

function save(formId){
    if(strUtil.isNull($("#" + formId).find("input[name='orgCname']").val())){
        layer.tips('机构名称不能为空！', '#orgCname', {tips: 1});
        return;
    }
    if(strUtil.isNull($("#" + formId).find("input[name='orgEname']").val())){
        layer.tips('机构标识不能为空！', '#orgEname', {tips: 1});
        return;
    }
	if(!$("#" + formId)[0].checkValidity()){
		alert("请先完善必输项和必选项！");
		return ;
	}
	var ename = $("#" + formId).find("input[name='orgEname']").val();
	$("#org_ifSaveDept").val("T");
	var index_submit = layer.confirm("您确定要增加机构["+ename+"]吗？", {btn: ["Yes","No"],closeBtn: 0,offset: "220px"}
		, function() {
			layer.close(index_submit);
			$.ajax({type:'post',url:common_submit_url,
				data:$("#"+formId).serialize(),dataType:'json',
				success: function(myData){
					console.log(myData);
					if(myData.flag=='T'){
						layer.alert('操作成功！');
						var newOrgId = myData.msg;
						if(!strUtil.isNull(newOrgId)){
							addUserDataGiveToOrg(newOrgId);
						}
						refresh();
					}else{
						layer.alert('操作失败！'+myData.msg);
					}
					$('#my_modal').modal('hide');
				}
			});
		}, function(){
		});

}

function cannel(id){
	$('#'+id).modal('hide');
}
function deleteAllRow(tableId){
	var tb = document.getElementById(tableId);
	if(tb){
	     var rowNum=tb.rows.length;
	     for (i=1;i<rowNum;i++){
	         tb.deleteRow(i);
	         rowNum=rowNum-1;
	         i=i-1;
	     }
     }
}

function refresh(){
    //window.location.reload();
	getTableData();
}

function toOrgUser(){
	if(id=="undefined" || id =="" || id == null){
		alert("请先选中一条数据！");
		return;
	}else{//orgUserDiv
		$("#orgUserDiv").load("${ctx}/template/uct/org-user-list.html",function(){getOrgUserListData();});
		$('#org_user_modal').modal('show');
	}
}

function getSysEname(){
	$.ajax({
		type:'get',
		url:common_list_url,
		data:{'pageName':'uct-sys','formName':'searchForm'},
		dataType:'json',
		success:function(myData){
			console.log(myData);
			if(myData){
	        	var htmlContent = "<option value=''>--请选择--</option>";
				for(var i = 0; i < myData.total; i++){
					htmlContent +="<option value='"+myData.rows[i].sysEname+"' >"+myData.rows[i].sysCname+"</option>"; 
				}
					$("#sysEname").html(htmlContent);
        	}
		}
	});
}
//20180806 验证机构标识是否存在
function checkOrgEnameIsUse(obj) {
    if(strUtil.isNull($(obj).val())){
        layer.tips('机构标识不能为空！', '#orgEname', {tips: 1});
        return;
	}
    var index_load = layer.msg("机构标识验证中，请稍后！");
    var checkReturn = false;
    $.getJSON(common_list_url, {"doCode":"orgList","orgEname":$(obj).val()}, function(myData){
        console.log(myData);
        if(myData.total>0){
            layer.msg("标识["+$(obj).val()+"]已存在，请更换其他标识！");
            $(obj).val("")
        }else{
            layer.msg("标识["+$(obj).val()+"]可以使用！");
		}
    });
}
//20180806 添加把新增机构授权给当前机构管理员用户的机构数据权限
function addUserDataGiveToOrg(orgId) {
    var msg_load = layer.msg("正在授权给机构管理员，请耐心等待...");
    var paramData = {};
    paramData["flag"]="add";
    paramData["giveType"]="org";
    paramData["objId"]=orgId;
    paramData["userId"]="${loginUser.userId}";
    $.ajax({
        type:"post",url:ctx+"/res/uct/admin/userDataGive",data: paramData,dataType:"json",
        success: function(myData){
            console.log(myData);
            if(myData.flag=="T"){
                layer.close(msg_load);
                layer.alert("授权给本机构管理员的新机构操作权限完成。需退出重新登录才能生效！");
            }else{
                layer.alert("授权给机构管理员，操作失败！");
            }
        }
    });


}
</script>   
    
    
    
    
  </div>
  <!-- /.content-wrapper -->
  <!-- foot start -->
       
  <%@include file="/common/plateform/foot.jsp" %>
  <!-- foot start -->
  

 
</body>
	






</html>
